package com.example.rouroudate.ui.history.ui

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import com.example.rouroudate.R
import com.example.rouroudate.ui.home.ui.CircleDate
import com.example.rouroudate.util.DisplayUtils

class HistoryItemView @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    View(context, attrs, defStyleAttr) {
    companion object {
        const val TAG = "HistoryItemView"
    }

    private var dateSize = 26

    private var hurtSize = 5

    private val hurtColor = ContextCompat.getColor(context,R.color.hurt_color)

    private val paintColor = ContextCompat.getColor(context,R.color.light_pink)

    private val textColor = ContextCompat.getColor(context,R.color.pink)

    private val rectSize = DisplayUtils.dp2px(10f)

    private val path = Path()

    private lateinit var dateRect: RectF

    private lateinit var hurtRect: RectF

    private lateinit var dateStr: String

    private var midPoint = 0f

    fun setDate(startDate: CircleDate) {
        dateSize = startDate.settingData.dateSize
        hurtSize = startDate.settingData.hurtSize
        dateStr = startDate.getStr()
        postInvalidate()
    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        strokeWidth = rectSize.toFloat()
        color = paintColor
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND
    }

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = textColor
        style = Paint.Style.FILL
        textSize = rectSize * 2f
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.d(TAG, "w = $w")
        val margin = DisplayUtils.dp2px(10f).toFloat()
        val outW = w - margin * 2
        midPoint = w / 2f
        dateRect = RectF(margin, margin, outW + margin, margin + outW)
        hurtRect = RectF(margin + rectSize, margin + rectSize, outW + margin - rectSize, margin + outW - rectSize)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let {
            val hurtAngle = hurtSize / 31f * 360
            val dateAngle = dateSize / 31f * 360
            paint.color = paintColor
            path.addArc(dateRect, 0f, dateAngle)
            it.drawPath(path, paint)
            paint.color = hurtColor
            path.reset()
            path.addArc(hurtRect, 0f, hurtAngle)
            it.drawPath(path, paint)

            val tw = textPaint.measureText(dateStr)
            val x = midPoint - tw / 2f
            val fontMetrics = textPaint.fontMetrics
            val th = fontMetrics.bottom - fontMetrics.top
            val y = midPoint + th / 3
            it.drawText(dateStr, x, y, textPaint)
        }
    }
}